﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using CashBankIn.ViewModels;
using SyedMehrozAlam.CustomControls;
using WcfService1.DTO;

namespace CashBankIn
{
    public partial class FrmAdd : Page
    {
        public CashBankInViewModel VM
        {
            get
            {
                return this.DataContext as CashBankInViewModel;
            }
        }

        public CashBankInWrapViewModel WrapViewModel
        {
            get { return (this.Resources["POW"] as CashBankInWrapViewModel); }
        }

        public FrmAdd()
        {
            InitializeComponent();
            this.Loaded += (x, y) =>
            {
                (this.Resources["POW"] as CashBankInWrapViewModel).CashBankInViewModel =
                     (CashBankInViewModel)this.DataContext;
            };

            this.cboBank.SelectionChanged += new SelectionChangedEventHandler(cboBank_SelectionChanged);
        }

        void cboBank_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
         
        }

    
        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void TextBox_GotFocus(object sender, System.Windows.RoutedEventArgs e)
        {
        	(sender as TextBox).SelectAll();
        }
     

        private void cb_invoice_LostFocus(object sender, System.Windows.RoutedEventArgs e)
        {
            var invoice = (sender as AutoCompleteComboBox).SelectedItem as SalesInvoiceDTO;
            if (invoice != null)
                if (WrapViewModel.CashBankInDetail != null) 
                    WrapViewModel.CashBankInDetail.Kredit = invoice.Total;
        }

        private void cboBank_LostFocus(object sender, RoutedEventArgs e)
        {
            var coa = (sender as AutoCompleteComboBox).SelectedItem as CoaDTO;
            if (VM.CashBankIn.CashBankAccount != null  )
            {
                VM.GetLastNoVoucher();
                if (VM.CashBankIn.CashBankInDetails.Count == 0)
                    WrapViewModel.AddCashBankInDetail(coa);
            }
        }

    }
}
